﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using System.Web;
using EluZSProvider;

namespace WebSys.UI
{
    public class Login : Page
    {
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            HttpContext.Current.Session["UserID"] = null;
            HttpContext.Current.Session["UserName"] = null;
            HttpContext.Current.Session["LoginUserName"] = null;
            ImageButton login = (ImageButton)FindControl("login");
            login.Click += new ImageClickEventHandler(login_Click);
            login.ImageUrl = Antu.Core.Components.SiteSettings.DefaultSiteSettings.DefaultThemeFolderMapPath + "Images/dl.gif";

        }
        protected void login_Click(object sender, EventArgs e)
        {
            TextBox UserName = (TextBox)FindControl("UserName");
            TextBox Password = (TextBox)FindControl("Password");
            EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
            string strName = UserName.Text.Trim();
            string strPwd = Password.Text.Trim();
            string uname = string.Empty;
            string upwd = string.Empty;
            if (strName.IndexOf(" ") > -1)
            {
                uname = strName.Replace(" ", "");
            }
            else {
                uname = strName;
            }
            if (strPwd.IndexOf(" ") > -1)
            {
                upwd = strPwd.Replace(" ", "");
            }
            else {
                upwd = strPwd;
            }
            User user = _EluZSDataProvider.GetUser(uname, upwd);
            

            if (string.IsNullOrEmpty(user.Id))
            {
                HttpContext.Current.Response.Write("<script>alert('用户名密码错误！');</script>");
                return;
            }
            else
            {
                if (user.Loginid != "admin")
                {
                    EluZSProvider.Rizhi rizhi = new EluZSProvider.Rizhi();
                    rizhi.Eventname = "系统登录";
                    rizhi.Eventuser = user.Id;
                    bool save = _EluZSDataProvider.SaveRizhi(rizhi);
                }
                HttpContext.Current.Session["UserID"] = user.Id;
                HttpContext.Current.Session["UserName"] = user.Loginid;
                HttpContext.Current.Session["LoginUserName"] = user.Name;
                HttpContext.Current.Response.Redirect("Index.aspx");
            }
        }
    }
}


